/****************************************************************************
**
** Copyright (C) 2010 Tieto Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Tieto Corporation (info.ostrava@tieto.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Tieto.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Tieto gives you certain additional
** rights.  These rights are described in the Tieto Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Tieto at info.ostrava@tieto.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef FEATURECLASSIFICATIONMODEL_H
#define FEATURECLASSIFICATIONMODEL_H

#include <QtCore/QVector>

#include "featuremodel.h"
#include "backgroundtask.h"
#include "image.h"

class FeatureClassificationModel;
class ImageModel;

/**
 * Background task for feature classification
 */
class FeatureClassificationTask : public BackgroundTask
{

    Q_OBJECT

public:

    /**
     * Constructor
     * @param model model
     */
    FeatureClassificationTask(FeatureClassificationModel &model, QObject *parent = 0);

protected:

    /**
     * Background task method
     */
    virtual void doRun();

    /**
     * Background task cancel method
     */
    virtual void doCancel();

private:

    /**
     * Features
     */
    QVector<QVariant> _features;
    /**
     * Model
     */
    FeatureClassificationModel& _model;
};


/**
 * Feature classification of indexed areas
 */

class FeatureClassificationModel : public FeatureModel
{

    Q_OBJECT

public:

    /**
     * Image IDs
     */
    enum ImageID {
        EIndexedAreas = 0,      // Indexed areas
        EIndexedAreasImage,     // Indexed areas image
        ELastImage
    };

    /**
     * Features
     */
    enum FeatureID {
        EImage = 0,             // image
        ETODO,                  // TODO
        EPerimeter,             // perimeter
        EXleft,                 // the most left coord
        EXright,                // the most right coord
        EYtop,                  // the most top coord
        EYbottom,               // the most bottom coord
        ELastFeature
    };

    /**
     * Moment features
     */
    enum MomentFeature {
        EM00 = ELastFeature,    // moment m0,0 content
        EM10,                   // moment m1,0
        EM01,                   // moment m0,1
        EXt,                    // x axis center of mass
        EYt,                    // y axis center of mass
        EU11,                   // moment u11
        EU20,                   // moment u20
        EU02,                   // moment u02
        EUmin,                  // moment umax
        EUmax,                  // moment umin
        ELastMoment
    };

    /**
     * Constructor
     */
    explicit FeatureClassificationModel(QObject *parent = 0);

    /**
     * Gets model
     * @return model
     */
    QStandardItemModel& model();

    /**
     * Gets image model
     * @return image model
     */
    QStandardItemModel &imageModel();

    /**
     * Resets model
     */
    void reset();

    /**
     * Commits model
     */
    void commit();

    /**
     * Feature classification
     * @return area indexing task
     */
    FeatureClassificationTask* featureClassification();

    /**
     * Gets indexed areas
     * @return indexed areas
     */
    IndexImage indexedAreas();

    /**
     * Gets indexed areas image
     * @return indexed areas image
     */
    ColorImage indexedAreasImage();

    /**
     * Updates area moment feature
     * @param p x moment power
     * @param q y moment power
     * @param x x coordinate
     @ @param y y coordinate
     * @param row feature row
     * @param feature feature
     * @return updated moment
     */
    double updateMomentFeature(const int p, const int q, const int x, const int y, const int row, const MomentFeature feature);

    /**
     * Updates center of mass feature
     * @param row feature row
     */
    void updateMassCenterFeature(const int row);
    
   /**
    * Updates area moment related to center of mass axis
     * @param p x moment power
     * @param q y moment power
     * @param x x coordinate
     * @param y y coordinate
     * @param row feature row
     * @param feature feature
     * @return updated moment
     */
    double updateMassCenterMomentFeature(const int p, const int q, const int x, const int y, const int row, const MomentFeature feature);

    /**
     * Updates min and max related to center of mass axis
     * @param row feature row
     */
    void updateMainMassCenterMomentFeature(const int row);
    
    /**
     * Updates extreme coord
     * @param x x coordinate
     * @param y y coordinate
     * @param row feature row
     */
    void updateExtremeCoordFeature(const int x, const int y, const int row);

    /**
     * Updates area perimeter
     * @param x x coordinate
     * @param y y coordinate
     * @param row feature row
     * @param feature feature
     */
    void updatePerimeterFeature(const int x, const int y, const int row);

    /**
     * Updates image feature
     */
    void updateImageFeature(const int x, const int y, const int row);

    /**
     * Updates TODO
     * @param row feature row
     */
    void updateTODO(const int row);

    /**
     * Externalize
     * @return externalized features
     */
    ItemTable externalize();

signals:

    /**
     * Features computed
     * @param features features
     */
    void featuresComputed(const ItemTable &features);

public slots:

    /**
     * Set indexed areas
     * @param image indexed areas
     */
    void setIndexedAreas(const IndexImage &image);

    /**
     * Sets indexed areas image
     * @param indexed areas image
     */
    void setIndexedAreasImage(const ColorImage &image);

private:

    /**
     * Computes area moment
     * m(p,q) = SS(x^p)(y^p)f(x,y)dxdy
     * @param p x moment power
     * @param q y moment power
     * @param x x coordinate
     * @param y y coordinate
     * @return moment
     */
    double computeMoment(const int p, const int q, const int x, const int y);

    /**
     * Computes main moment related to mass center axis
     * @param u11 moment u11
     * @param u20 moment u20
     * @param u02 moment u02
     * @param sign sign
     */
    double computeMainMassCenterMoment(const double u11, const double u20, const double u02, const double sign);
    
    /**
     * Is pixel is in area?
     * @return true, if pixel is in area
     */
    bool isArea(const int x, const int y);

    /**
     * Updates extreme coord
     * @param coord coordinate
     * @param row feature row
     * @param feature feature
     * @param extreme 0 - minimum, 1 - maximum
     */
    void updateExtremeCoord(const int coord, const int row, const FeatureID feature, const int extreme=0);

    /**
     * Proxy model
     */
    QStandardItemModel *proxyModel;

    /**
     * Image model
     */
    ImageModel *_imageModel;

};

#endif // FEATURECLASSIFICATIONMODEL_H
